class Animal {
    public String name;
    public int age;
    public Animal() {
        eat();
        System.out.println("不带参数的构造方法！Animal");
    }

    public Animal(String name,int age){
        this.name = name;
        this.age = age;
    }
    public void eat() {
        System.out.println(this.name+" 正在吃饭！");
    }
}
class Dog extends Animal {
    public Dog() {
        System.out.println("不带参数Dog！");
    }
    public Dog(String name,int age) {
        super(name,age);
    }

    public void bark() {
        System.out.println(this.name +" 正在旺旺旺！！");
    }

    @Override
    public void eat() {
        System.out.println(this.name+ " 正在吃狗粮！");
    }
}
class Cat extends Animal {
    public Cat() {
        super("haha",10);
    }
    public void mimi() {
        System.out.println(this.name+" 正在喵喵喵！");
    }

    public void eat() {
        System.out.println(this.name+" 正在吃猫粮！");
    }
}

public class Test {

    public static void main6(String[] args) {
        Animal animal = new Dog();

    }

    public static void main5(String[] args) {
        Animal animal = new Dog("wangwang",1);
        Dog dog = (Dog)animal;
        dog.bark();

        if(animal instanceof Cat) {
            Cat cat = (Cat) animal;
            cat.mimi();
        }

    }


    public static void testFunc(Animal animal) {
        animal.eat();
    }

    public static void main(String[] args) {
        Dog dog = new Dog("wangwang",1);
        testFunc(dog);
        System.out.println("===========");
        Cat cat = new Cat();
        testFunc(cat);
    }



    public static Animal testFunc2() {
        return new Dog();
    }

    public static Dog testFunc3() {
        return new Dog();
    }

    public static void main3(String[] args) {
        Animal animal = testFunc3();

        Dog dog = new Dog("wangwang",1);
        testFunc(dog);

    }

    public static void main2(String[] args) {
        /*Dog dog = new Dog("wangwang",1);
        Animal animal = dog;*/
        Animal animal = new Dog("wangwang",1);
        animal.eat();
    }
    public static void main1(String[] args) {
        Dog dog = new Dog("wangwang",1);
        dog.eat();

        Cat cat = new Cat();
        cat.eat();

    }
}

/*
public class TestAnimal {
    public static void main(String[] args) {
        Cat cat = new Cat("元宝",2);
        Dog dog = new Dog("小七", 1);
// 向上转型
        Animal animal = cat;
        animal.eat();
        animal = dog;
        animal.eat();
// 编译失败，编译时编译器将animal当成Animal对象处理
// 而Animal类中没有bark方法，因此编译失败
// animal.bark();
// 向上转型
// 程序可以通过编程，但运行时抛出异常---因为：animal实际指向的是狗
// 现在要强制还原为猫，无法正常还原，运行时抛出：ClassCastException
        cat = (Cat)animal;
        cat.mew();
// animal本来指向的就是狗，因此将animal还原为狗也是安全的
        dog = (Dog)animal;
        dog.bark();
    }
}*/

/*
public class TestAnimal {
    public static void main(String[] args) {
        Cat cat = new Cat("元宝",2);
        Dog dog = new Dog("小七", 1);
// 向上转型
        Animal animal = cat;
        animal.eat();
        animal = dog;
        animal.eat();
        if(animal instanceof Cat){
            cat = (Cat)animal;
            cat.mew();
        }
        if(animal instanceof Dog){
            dog = (Dog)animal;
            dog.bark();
        }
    }
}
*/
